package ourFilter;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.image.renderable.ParameterBlock;
import java.io.StreamCorruptedException;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

import com.sun.media.jai.widget.DisplayJAI;

import pipe.Pipe;
import filter.Sink;

public class OurSink extends Sink<PlanarImage> {

	public OurSink(Pipe<PlanarImage> input) {
		super(input);
		input.setOut(this);
	}

	@Override
	public void write(PlanarImage img) throws StreamCorruptedException { 
		//showGui(img);
		
		JAI.create("filestore", img, "result.jpg", "JPEG");
	}

	@Override
	public void takeForm(Pipe<PlanarImage> pipe) {
		PlanarImage img = null;
		try {
			img = pipe.read();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		}
		//showGui(img);
		
		JAI.create("filestore", img, "result.jpg", "JPEG");
	}
	private void showGui(PlanarImage image){
		//Display info
		String imageInfo = "Dimensions: " + image.getWidth() + "x" + image.getHeight() + " Bands:" + image.getNumBands();
		
		JFrame frame = new JFrame();
		frame.setTitle("Display Result Image");

		// Get the JFrameí ContentPane.
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BorderLayout());


		// Create an instance of DisplayJAI.
		DisplayJAI dj = new DisplayJAI(image);

		// Add to the JFrameí ContentPane an instance of JScrollPane
		// containing the DisplayJAI instance.
		contentPane.add(new JScrollPane(dj), BorderLayout.CENTER);

		// Add a text label with the image information.
		contentPane.add(new JLabel(imageInfo), BorderLayout.SOUTH);

		// Set the closing operation so the application is finished.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(image.getWidth() + 10, image.getHeight() + 50); // adjust the frame size.
		frame.setVisible(true); // show the frame.
	}



}
